package vsvm.graphics.charts;

import java.awt.Color;
import java.awt.GradientPaint;

import javax.swing.JList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import vsvm.data.model.AbstractAttribute;

public class FeatureImportanceHistogram{

	private double[] data;
	private ChartPanel chartPanel;
	private String name;
	
	
	private static final long serialVersionUID = 100L;

	public FeatureImportanceHistogram (double[] data, String name, JList lstAttributes) {

		this.data = data;		
		this.name = name;
        CategoryDataset dataset = createDataset(lstAttributes);       
        JFreeChart chart = createChart(dataset);
        
        chartPanel = new ChartPanel(chart, false);

    }


    private CategoryDataset createDataset(JList lstAttributes) {//static CategoryDataset

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(int i = 0; i < data.length; i++)
        {
        	dataset.addValue(data[i], name, 
    				((AbstractAttribute)lstAttributes.getModel().getElementAt(i)).getName());
        }
        return dataset;
        
    }
    
    private JFreeChart createChart(CategoryDataset dataset) {//static CategoryDataset
        
        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(//BarChart(
            "Feature importance histogram",       // chart title
            "Number",               // domain axis label
            "Importance",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.white);

        
        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());

        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(true); //false
        
        // set up gradient paints for series...
        GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 
                0.0f, 0.0f, new Color(0, 0, 64));
//       GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 
//                0.0f, 0.0f, new Color(0, 64, 0));
//        GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 
//                0.0f, 0.0f, new Color(64, 0, 0));
        renderer.setSeriesPaint(0, gp0);
//        renderer.setSeriesPaint(1, gp1);
       // renderer.setSeriesPaint(2, gp0);

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
                CategoryLabelPositions.createUpRotationLabelPositions(
                        0));//Math.PI / 6.0));
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return chart;
        
    }


	public ChartPanel getChartPanel() {
		return chartPanel;
	}

}

